*	the 14:	relread0 relfrnd0 relblgd0 relpryr0 relgdpres0 relprot0 rellive0 relcmfrt0 relpeace0 relnotdly0 relwfrnd0 relwhllf0 relgosee0 relothimp0 
*	the 7:  relpryr0 relgdpres0 relprot0 rellive0 relcmfrt0 relpeace0   relwhllf0
*-	the OTHER 7 	relgosee0 relread0 relfrnd0 relblgd0       relnotdly0 relwfrnd0   relothimp0 
 *** create documentation
 desc // list all variables 
 tab1 interv fgender race5c educ coledu01  loch1m0  // check coding for categoricals 
 tab1 interv fgender race5c educ coledu01  loch1m0  , nolabel
label dir anxdep0
label list RO14MI_00

*	Table 1. Descriptives of the analyzed variables religiosity and anxiety, and standardized regression coefficients of anxiety regressed on religiosity, without and with measurement error 
tab fgender coledu01   
* to get SDs and COV and Corr for tracing rule 
sum fgender coledu01   
sem  fgender coledu01   , standardized
reg fgender coledu01 , beta 
sum  anxdep0 rel7it age0 chronicn relgosee0	hlth_0b rel7hlt // 

*		***** 	
*		***** 	
*	Table 2. Results of different analytic tools applied to the `Gender -> Education' model 
*	Chi-square A
tab  fgender coledu01   , row col chi 

*	t-test A
 ttest coledu01 , by(fgender)
 
 *	Correlation between observablesA
 pwcorr  fgender coledu01  , sig star(.05)
 *pwcorr anxdep0 rel7it age0 chronicn relgosee0	rel7byhlt hlth_0 , sig star(.05)
 
*	Correlation –corrected for attenuationB
display Corr/sqrt(.RelX*.RelY ) " Corr/SQRT(RelX*RelY)"

*	True Correlation between latentsC	8.13491 = SDRel  .4528248  =SDAnx *  
* sem (rel7it anxdep0) // 66.5921  .2046482
 
 // to see VARIANCES 
 sem ( fgender coledu01) // var(fgender)|   .1935583 var(coledu01)|   .2211068   

 display .05*.1935583 " 5% VarMeasError fgender" //.00967792 
 display .1*.2211068  " 10% VarMeasError coledu01 " //.02211068 
 display 0.0138 /sqrt(.95*.90 ) " Corr/SQRT(RelX*RelY)" //.01492437 
 
sem (LVFem@1  -> fgender) (LVColl@1 -> coledu01) (LVFem LVColl) ,  var(e.fgender@0  e.coledu01@0) // 0 MEasErr
sem (LVFem@1  -> fgender) (LVColl@1 -> coledu01) (LVFem LVColl) ,  var(e.fgender@0 e.coledu01@0) standardized // 0 MEasErr corr=.0138132

sem (LVFem@1  -> fgender) (LVColl@1 -> coledu01) (LVFem LVColl) ,  var(e.fgender@.00967792  e.coledu01@.02211068  ) 
sem (LVFem@1  -> fgender) (LVColl@1 -> coledu01) (LVFem LVColl) ,  var(e.fgender@.00967792  e.coledu01@.02211068  ) standardized 

*	Regression/path analysis - observables
reg coledu01	fgender
sem coledu01 <-	fgender  // 	.0138132 
sem coledu01 <-	fgender, standardized  // 	.0138132 
logit coledu01	fgender 
logit coledu01	fgender , or

gsem (coledu01 <-fgender),   family(ordinal) link(logit)
* EMERGED after: IV possible behind GENDER ..... 
 pwcorr  fgender coledu01 rel7it anxdep0  age0 hlth_0  fg0 a1c0   fatstch0 PAstch0 chronicn interv loch1m0  , sig star(.05)
 sem  (  hlth_0  -> fgender) (fgender  -> coledu01), cov(e.fgender*e.coledu01)	// p=.168 interv  maybe too 
 sem  (  hlth_0  -> fgender) (fgender  -> coledu01), cov(e.fgender*e.coledu01)	standardized
 
*	Logistic regression - observables
logistic  coledu01	fgender
logistic  coledu01	fgender, or
gsem (coledu01 <-fgender),   family(ordinal) link(logit)

*	Path analysis - latent GenderC
sem (LVFem@1  -> fgender) (LVFem -> coledu01) ,  var(e.fgender@.00967792 ) 
sem (LVFem@1  -> fgender) (LVFem -> coledu01) ,  var(e.fgender@.00967792 )  standardized // .0141721

*		***** 	
*	Table 3. Two variable models: effects between Religiosity (X) to Anxiety (Y)
 *	Correlation between observablesA
 pwcorr  rel7it anxdep0  , sig star(.05)
 sem rel7it anxdep0 
 sem rel7it anxdep0 ,standardized
 
 *	1. Regression/path model	Religiosity -> Anxiety 
sem (rel7it -> anxdep0)   // 0 MEasErr

*	Latent Religiosity A  -> Anxiety
sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@0 e.rel7it@0) // 0 MEasErr
sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@0 e.rel7it@0) standardized // 0 MEasErr
* 
sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@0 e.rel7it@13.31842 ) 
sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@0 e.rel7it@13.31842 ) standardized

sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@.04092964 e.rel7it@13.31842 ) 
sem (LVAnx@1  -> anxdep0) (LVRel@1 -> rel7it) (LVRel -> LVAnx) ,  var(e.anxdep0@.04092964 e.rel7it@13.31842 ) standardized

*	'True' Religiosity B  -> Anxiety
*	relpryr0 relgdpres0 relprot0 rellive0 relcmfrt0 relpeace0   relwhllf0
sem (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> anxdep0)  ,  var(LVRel@1)   // Raykov type SOME covs make effect <SIG!!!, some >!!! ; Prob > chi2 = 0.6858
 
 sem (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> anxdep0)  ,  var(LVRel@1) cov(e.relpryr0*e.relgdpres0) cov(e.relprot0*e.relcmfrt0) cov(e.relprot0*e.relpeace0) cov(e.rellive0*e.relwhllf0) cov(e.relcmfrt0*e.relpeace0) standardized // Raykov type SOME covs make effect <SIG!!!, some >!!! ; Prob > chi2 = 0.6858
 
estat mindices
* code for Raykov reliability: do error covs affect it? Should...
sem (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) ,  var(LVRel@1) 

sem (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) ,  var(LVRel@1) cov(e.relpryr0*e.relgdpres0) cov(e.relprot0*e.relcmfrt0) cov(e.relprot0*e.relpeace0) cov(e.rellive0*e.relwhllf0) cov(e.relcmfrt0*e.relpeace0)  
sem, coeflegend
* _b[relpryr0:LVRel] _b[relgdpres0:LVRel] _b[relprot0:LVRel] _b[rellive0:LVRel] _b[relcmfrt0:LVRel] _b[relpeace0:LVRel] _b[relwhllf0:LVRel]
* _b[/var(e.relpryr0)] _b[/var(e.relgdpres0)] _b[/var(e.relprot0)] _b[/var(e.rellive0)] _b[/var(e.relcmfrt0)] _b[/var(e.relpeace0)] _b[/var(e.relwhllf0)]
* (sum(l's))^2/((sum(l's)^2 + sum(vars e's) ) 

nlcom ((_b[relpryr0:LVRel] + _b[relgdpres0:LVRel] + _b[relprot0:LVRel] + _b[rellive0:LVRel] + _b[relcmfrt0:LVRel] + _b[relpeace0:LVRel] + _b[relwhllf0:LVRel])^2)/((_b[relpryr0:LVRel] + _b[relgdpres0:LVRel] + _b[relprot0:LVRel] + _b[rellive0:LVRel] + _b[relcmfrt0:LVRel] + _b[relpeace0:LVRel] + _b[relwhllf0:LVRel])^2 + _b[/var(e.relpryr0)] + _b[/var(e.relgdpres0)] + _b[/var(e.relprot0)] + _b[/var(e.rellive0)] + _b[/var(e.relcmfrt0)] + _b[/var(e.relpeace0)] + _b[/var(e.relwhllf0)])


*	largest 
*	cov(e.relpryr0,e.relgdpres0)	40.828
*	cov(e.relprot0,e.relcmfrt0)	25.477
*	cov(e.relprot0,e.relpeace0)	24.663
*	cov(e.rellive0,e.relwhllf0)	24.379
*	cov(e.relcmfrt0,e.relpeace0)	23.479

*	2. Bi-directional effects A	Religiosity -> Anxiety	Anxiety -> Religiosity
*	Religiosity -> Anxiety + Anxiety -> Religiosity
 sem  (anxdep0  relgosee0 -> rel7it) (rel7it  chronicn  age0  -> anxdep0) , nocapslatent  //standardized 
* cov(e.anxdep0*e.rel7it) pbsintrf0 
estat stable

*	'True' Religiosity B  -> Latent Anxiety A
* sem  (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> anxdep0) (anxdep0  relgosee0 -> LVRel ) (LVRel  chronicn  age0  -> anxdep0) , nocapslatent latent(LVRel)     //standardized var(LVRel@1)   cov(e.LVRel*e.anxdep0)
 sem  (LVAnx@1  -> anxdep0) (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> LVAnx) (LVAnx  relgosee0 -> LVRel ) (LVRel  chronicn  age0  -> LVAnx) , nocapslatent latent(LVRel LVAnx)  var(e.anxdep0@.04092964)     
 estat teffects
 
*	Latent Anxiety A  -> 'True' Religiosity B
 sem  (anxdep0  relgosee0 -> rel7it) (rel7it  chronicn  age0  -> anxdep0) , nocapslatent  //standardized 
 
*	2. `Instrumental variable' regresionB	Religiosity -> Anxiety
* covs for tracing rule IV βY.X = σZY /σZX
 sem relgosee0 rel7it anxdep0 
 
*	ivregress 2sls Y (X = IV)
	ivregress 2sls anxdep0 (rel7it = relgosee0)
 sem  (  relgosee0 -> rel7it) (rel7it  -> anxdep0), cov(e.rel7it*e.anxdep0)	
 sem  (  relgosee0 -> rel7it) (rel7it  -> anxdep0), cov(e.rel7it*e.anxdep0)	standardized
*	'True' Religiosity B  -> Anxiety A
 sem  (LVAnx@1  -> anxdep0) (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> LVAnx) ( relgosee0 -> LVRel ) (LVRel  -> LVAnx) , nocapslatent latent(LVRel LVAnx)  var(e.anxdep0@.04092964)   cov(e.LVRel*e.LVAnx)  
 
sem  (LVAnx@1  -> anxdep0) (LVRel -> relpryr0) (LVRel -> relgdpres0)  (LVRel -> relprot0) (LVRel -> rellive0) (LVRel -> relcmfrt0) (LVRel -> relpeace0) (LVRel -> relwhllf0) (LVRel -> LVAnx) ( relgosee0 -> LVRel ) (LVRel  -> LVAnx) , nocapslatent latent(LVRel LVAnx)  var(e.anxdep0@.04092964)   cov(e.LVRel*e.LVAnx) standardized 
*	Table 4. Three variable models: effects from Religiosity (X) to Anxiety (Y), modified by and through self-rated Health (Mediator/Moderator)
*	1. Co-predictors	Religiosity -> Anxiety	Health -> Anxiety
reg anxdep0 rel7it hlth_0  
sem  (rel7it hlth_0 -> anxdep0) // , cov(e.rel7it*e.anxdep0)	
sem  (rel7it hlth_0 -> anxdep0),	standardized //  cov(e.rel7it*e.anxdep0)
  
*	2. Moderation/interaction	Religiosity -> Anxiety	Health -> Anxiety	Religiosity*Health -> Anxiety
reg anxdep0 rel7it hlth_0b rel7hlt  
sem  (rel7it hlth_0b  rel7hlt-> anxdep0) // , cov(e.rel7it*e.anxdep0)	
sem  (rel7it hlth_0b  rel7hlt-> anxdep0),	standardized //  cov(e.rel7it*e.anxdep0)

*	3. Mediation 	DE c': Religiosity -> Anxiety	IE a*b: Religiosity -> Anxiety	TE c: Religiosity -> Anxiety	a: Religiosity ->  Health	b:  Health -> Anxiety
sem (hlth_0b <-  rel7it) (anxdep0  <- rel7it hlth_0b ) , nocapslatent standardized // p = 0.463  dep4w0 dwndep015r

estat teffects
estat teffects, standardized

*	to get IE and TE by hand:
sem, coeflegend
*	DE 
nlcom _b[anxdep0 :rel7it]
*	IE
nlcom _b[hlth_0:rel7it]*_b[anxdep0:hlth_0]
*	TE
nlcom _b[anxdep0 :rel7it] + _b[hlth_0:rel7it]*_b[anxdep0:hlth_0]

*	4. Mediation and moderationA  	Religiosity*Health -> Anxiety	tDE c': Religiosity -> Anxiety	pIE a*b: Religiosity -> Anxiety	TE c: Religiosity -> Anxiety	a: Religiosity ->  Health	b:  Health -> Anxiety
*	relbychr 
sem (hlth_0b  <-  rel7it) (anxdep0  <- rel7it hlth_0b rel7hlt) , nocapslatent  standardized
estat teffects
estat teffects, standardized
*	DE 
nlcom _b[anxdep0 :rel7it]
*	IE
nlcom _b[hlth_0:rel7it]*_b[anxdep0:hlth_0]
*	TE
nlcom _b[anxdep0 :rel7it] + _b[hlth_0:rel7it]*_b[anxdep0:hlth_0]


*		***** 	

*		***** 	

*		***** 	

*		***** 	

*		***** 	

*		***** 	


*		***** 	

*		***** 	

*		***** 	

*		***** 	

*		***** 	

*		***** 	


*		***** 	

*		***** 	

*		***** 	
